package com.kuang.sync.singledown;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kuang.mapper.*;
import com.kuang.pojo.*;
import com.kuang.utils.ControllerLogUtils;
import com.kuang.utils.MQTTConnect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SingleDownServiceImpl implements SingleDownService {

    @Value("${mqtt.sfurl}")
    String url;

    @Resource
    MQTTConnect mqttConnect;
    @Resource
    AstControllerPoweronMapper astControllerPoweronMapper;
    @Resource
    AstControllerHeartMapper astControllerHeartMapper;
    /*
    设备本地开井实时
     */
    @Resource
    AstLocalOpeningWellMapper astLocalOpeningWellMapper;
    /*
    设备远程开井实时
    */
    @Resource
    AstRemoteOpeningWellMapper astRemoteOpeningWellMapper;
    /*
    设备本地开井
    */
    @Resource
    AstLocalOpenWellMapper astLocalOpenWellMapper;
    /*
    设备本地关井
    */
    @Resource
    AstLocalCloseWellMapper astLocalCloseWellMapper;

    Logger logger = Logger.getLogger(SingleDownServiceImpl.class);

    //设备的注册信息
    @Override
    public String postControllerRegisterSrv(String id, JSONObject preant) throws MqttException {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/ImportControllerRegister";

        preant.put("device_code", id);
        String requestBody = JSON.toJSONString(preant);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON
            logger.info("信息:" + JSON.toJSONString(responseBody));

            String success = jsonObject.getString("success");
            String msg = jsonObject.getString("msg");

            if (StringUtils.equals("true", success)) {
                JSONObject data = jsonObject.getJSONObject("data");
                boolean pub = mqttConnect.pub("/single/down/" + id, JSON.toJSONString(data));//发布主题
                int i = ControllerLogUtils.setMsgLog("post", "/single/down/ImportControllerRegister",
                        "设备的注册信息", msg, JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            } else {
                int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/ImportControllerRegister",
                        "设备的注册信息", msg, JSON.toJSONString(preant), JSON.toJSONString(responseBody));
                logger.info("错误信息:" + msg);

                //发送返回讯息
                JSONObject toJson = new JSONObject();
                JSONObject data = new JSONObject();
                toJson.put("cmd", MapUtils.getIntValue(preant, "cmd"));
                toJson.put("ver", MapUtils.getString(preant, "ver"));
                data.put("state", "0");
                data.put("dev_register_no", "");
                toJson.put("data", data);
                boolean pub = mqttConnect.pub("/single/down/" + id, JSON.toJSONString(toJson));//发布主题
            }


        } catch (Exception e) {
            int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/ImportControllerRegister",
                    "设备的注册信息", e.getMessage(), JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            logger.info("错误信息:" + e.getMessage());

            //发送返回讯息
            JSONObject toJson = new JSONObject();
            JSONObject data = new JSONObject();
            toJson.put("cmd", MapUtils.getIntValue(preant, "cmd"));
            toJson.put("ver", MapUtils.getString(preant, "ver"));
            data.put("state", "0");
            data.put("dev_register_no", "");
            toJson.put("data", data);
            boolean pub = mqttConnect.pub("/single/down/" + id, JSON.toJSONString(toJson));//发布主题

            return null;
        }

        return null;
    }


    //服务器远程开井返会的讯息
    @Override
    public String postRemoteOpenWellSrv(String id, JSONObject preant) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/getOpenWellResp";

        preant.put("device_code", id);
        String requestBody = JSON.toJSONString(preant);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            Map<String, Object> map = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON

            String success = MapUtils.getString(map, "success", "");
            String msg = MapUtils.getString(map, "msg", "");
            if (StringUtils.equals("false", success)) {
                int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/getOpenWellResp",
                        "服务器远程开井返会的讯息", msg, JSON.toJSONString(preant), JSON.toJSONString(responseBody));
                log.info("错误信息:" + msg);
                return null;
            }

        } catch (Exception e) {
            int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/getOpenWellResp",
                    "服务器远程开井返会的讯息", e.getMessage(), JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            log.info("错误信息:" + e.getMessage());
            return null;
        }
        int i = ControllerLogUtils.setMsgLog("post", "/single/down/getOpenWellResp",
                "服务器远程开井返会的讯息", "成功", JSON.toJSONString(preant), JSON.toJSONString(responseBody));
        log.info("信息:" + JSON.toJSONString(preant));
        return null;
    }

    //服务器远程关井返会的讯息
    @Override
    public String postRemoteCloseWellSrv(String id, JSONObject preant) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/getCloseWellResp";

        preant.put("device_code", id);
        String requestBody = JSON.toJSONString(preant);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            Map<String, Object> map = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON

            String success = MapUtils.getString(map, "success", "");
            String msg = MapUtils.getString(map, "msg", "");
            if (StringUtils.equals("false", success)) {
                int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/getCloseWellResp",
                        "服务器远程关井返会的讯息", msg, JSON.toJSONString(preant), JSON.toJSONString(responseBody));
                log.info("错误信息:" + msg);
                return null;
            }

        } catch (Exception e) {
            int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/getCloseWellResp",
                    "服务器远程关井返会的讯息", e.getMessage(), JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            log.info("错误信息:" + e.getMessage());
            return null;
        }

        int i = ControllerLogUtils.setMsgLog("post", "/single/down/getCloseWellResp",
                "服务器远程关井返会的讯息", "成功", JSON.toJSONString(preant), JSON.toJSONString(responseBody));

        return null;
    }

    /**
     * //2.10 获取设备端信息 cmd=controller_info
     *
     * @param id
     * @param preant
     * @return
     */
    @Override
    public String postControllerInfoSrv(String id, JSONObject preant) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/getDeviceInfoResp";

        preant.put("device_code", id);
        String requestBody = JSON.toJSONString(preant);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            Map<String, Object> map = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON

            String success = MapUtils.getString(map, "success", "");
            String msg = MapUtils.getString(map, "msg", "");
            if (StringUtils.equals("false", success)) {
                int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/getDeviceInfoResp",
                        "获取设备端信息", msg, JSON.toJSONString(preant), JSON.toJSONString(responseBody));
                log.info("错误信息:" + msg);
                return null;
            }

        } catch (Exception e) {
            int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/getDeviceInfoResp",
                    "获取设备端信息", e.getMessage(), JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            log.info("错误信息:" + e.getMessage());
            return null;
        }

        int i = ControllerLogUtils.setMsgLog("post", "/single/down/getDeviceInfoResp",
                "获取设备端信息", "成功", JSON.toJSONString(preant), JSON.toJSONString(responseBody));
        log.info("错误信息:" + JSON.toJSONString(responseBody));
        return null;
    }


    @Override
    public String postControllerPoweron(AstControllerPoweron astControllerPoweron) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/controllerPoweron";
        String controller_name = "控制器-设备端开机";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ObjectMapper objectMapper = new ObjectMapper();

        JSONObject data = objectMapper.convertValue(astControllerPoweron, JSONObject.class);
        String create_time = simpleDateFormat.format(astControllerPoweron.getCreate_time());
        data.put("create_time", create_time);

        String requestBody = JSON.toJSONString(data);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON
            logger.info("信息:" + JSON.toJSONString(responseBody));

            String success = jsonObject.getString("success");
            String msg = jsonObject.getString("msg");
            if (StringUtils.equals("true", success)) {
                int i = ControllerLogUtils.setMsgLog("post", address,
                        controller_name, msg, requestBody, responseBody);
                astControllerPoweron.setSuccess("true");
                int update = astControllerPoweronMapper.update(astControllerPoweron);
            } else {
                logger.info("错误信息:" + msg);
                int i = ControllerLogUtils.setErrorMsgLog("post", address,
                        controller_name, msg, requestBody, responseBody);
                astControllerPoweron.setSuccess("false");
                int update = astControllerPoweronMapper.update(astControllerPoweron);
            }

        } catch (Exception e) {
            logger.info("错误信息:" + e.getMessage());
            int i = ControllerLogUtils.setErrorMsgLog("post", address,
                    controller_name, e.getMessage(), requestBody, responseBody);
            astControllerPoweron.setSuccess("false");
            int update = astControllerPoweronMapper.update(astControllerPoweron);
        }

        return null;
    }

    @Override
    public String postControllerHeart(AstControllerHeart astControllerHeart) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down//controllerHeart";
        String controller_name = "控制器-设备端心跳";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ObjectMapper objectMapper = new ObjectMapper();

        JSONObject data = objectMapper.convertValue(astControllerHeart, JSONObject.class);
        String create_time = simpleDateFormat.format(astControllerHeart.getCreate_time());
        data.put("create_time", create_time);

        String requestBody = JSON.toJSONString(data);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON
            logger.info("信息:" + JSON.toJSONString(responseBody));

            String success = jsonObject.getString("success");
            String msg = jsonObject.getString("msg");
            if (StringUtils.equals("true", success)) {
//                int i = ControllerLogUtils.setMsgLog("post", address,
//                        controller_name, msg, requestBody, responseBody);
                astControllerHeart.setSuccess("true");
                int update = astControllerHeartMapper.update(astControllerHeart);
            } else {
                logger.info("错误信息:" + msg);
//                int i = ControllerLogUtils.setErrorMsgLog("post", address,
//                        controller_name, msg, requestBody, responseBody);
                astControllerHeart.setSuccess("false");
                int update = astControllerHeartMapper.update(astControllerHeart);
            }

        } catch (Exception e) {
            logger.info("错误信息:" + e.getMessage());
//            int i = ControllerLogUtils.setErrorMsgLog("post", address,
//                    controller_name, e.getMessage(), requestBody, responseBody);
            astControllerHeart.setSuccess("false");
            int update = astControllerHeartMapper.update(astControllerHeart);
        }

        return null;
    }


    /**
     * 管理机-设备本地、远程开井实时
     *
     * @param
     * @return
     */
    @Override
    public String postRealTimeRecords(String type, Object dataModel) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/realTimeRecords";
        String controller_name = "推送控制器-（本地/远程）开井实时记录给平台";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ObjectMapper objectMapper = new ObjectMapper();
        AstRemoteOpeningWell astRemoteOpeningWell = new AstRemoteOpeningWell();
        AstLocalOpeningWell astLocalOpeningWell = new AstLocalOpeningWell();
        JSONObject data;
        String create_time = "";
        String id = "";

        if (StringUtils.equals("local", type)) {
            astLocalOpeningWell = (AstLocalOpeningWell) dataModel;
            create_time = simpleDateFormat.format(astLocalOpeningWell.getCreate_time());
            id = astLocalOpeningWell.getAst_local_opening_well_id();

        } else if (StringUtils.equals("remote", type)) {
            astRemoteOpeningWell = (AstRemoteOpeningWell) dataModel;
            create_time = simpleDateFormat.format(astRemoteOpeningWell.getCreate_time());
            id = astRemoteOpeningWell.getAst_remote_opening_well_id();
        } else {
            return null;
        }

        data = objectMapper.convertValue(dataModel, JSONObject.class);
        data.put("create_time", create_time);
        data.put("id", id);

        if (StringUtils.equals("remote", type)) {
            data.put("user_ic_card", "无");
            data.put("user_phone", "");
        }

        String requestBody = JSON.toJSONString(data);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON
            logger.info("信息:" + JSON.toJSONString(responseBody));

            String success = jsonObject.getString("success");
            String msg = jsonObject.getString("msg");
            if (StringUtils.equals("true", success)) {
                int i = ControllerLogUtils.setMsgLog("post", address,
                        controller_name, msg, requestBody, responseBody);
                if (StringUtils.equals("local", type)) {
                    astLocalOpeningWell.setSuccess("true");
                    int update = astLocalOpeningWellMapper.update(astLocalOpeningWell);
                } else {
                    astRemoteOpeningWell.setSuccess("true");
                    int update = astRemoteOpeningWellMapper.update(astRemoteOpeningWell);
                }

            } else {
                logger.info("错误信息:" + msg);
                int i = ControllerLogUtils.setErrorMsgLog("post", address,
                        controller_name, msg, requestBody, responseBody);

                if (StringUtils.equals("local", type)) {
                    astLocalOpeningWell.setSuccess("flase");
                    int update = astLocalOpeningWellMapper.update(astLocalOpeningWell);
                } else {
                    astRemoteOpeningWell.setSuccess("flase");
                    int update = astRemoteOpeningWellMapper.update(astRemoteOpeningWell);
                }

            }

        } catch (Exception e) {
            logger.info("错误信息:" + e.getMessage());
            int i = ControllerLogUtils.setErrorMsgLog("post", address,
                    controller_name, e.getMessage(), requestBody, responseBody);
            if (StringUtils.equals("local", type)) {
                astLocalOpeningWell.setSuccess("flase");
                int update = astLocalOpeningWellMapper.update(astLocalOpeningWell);
            } else {
                astRemoteOpeningWell.setSuccess("flase");
                int update = astRemoteOpeningWellMapper.update(astRemoteOpeningWell);
            }

            return null;
        }

        return null;
    }

    @Override
    public String postLocalOpen(AstLocalOpenWell astLocalOpenWell) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/localOpenWell";
        String controller_name = "iot推送控制器-本地开井给平台";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ObjectMapper objectMapper = new ObjectMapper();
        JSONObject data;
        String create_time = "";

        create_time = simpleDateFormat.format(astLocalOpenWell.getCreate_time());
        data = objectMapper.convertValue(astLocalOpenWell, JSONObject.class);
        data.put("create_time", create_time);

        String requestBody = JSON.toJSONString(data);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON
            logger.info("信息:" + JSON.toJSONString(responseBody));

            String success = jsonObject.getString("success");
            String msg = jsonObject.getString("msg");

            if (StringUtils.equals("true", success)) {
                int i = ControllerLogUtils.setMsgLog("post", address,
                        controller_name, msg, requestBody, responseBody);
                astLocalOpenWell.setSuccess("true");
                int update = astLocalOpenWellMapper.update(astLocalOpenWell);

            } else {
                logger.info("错误信息:" + msg);
                int i = ControllerLogUtils.setErrorMsgLog("post", address,
                        controller_name, msg, requestBody, responseBody);
                astLocalOpenWell.setSuccess("flase");
                int update = astLocalOpenWellMapper.update(astLocalOpenWell);
            }

        } catch (Exception e) {
            logger.info("错误信息:" + e.getMessage());
            int i = ControllerLogUtils.setErrorMsgLog("post", address,
                    controller_name, e.getMessage(), requestBody, responseBody);
            astLocalOpenWell.setSuccess("flase");
            int update = astLocalOpenWellMapper.update(astLocalOpenWell);

            return null;
        }

        return null;
    }

    @Override
    public String postLocalClose(AstLocalCloseWell astLocalCloseWell) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/localCloseWell";
        String controller_name = "iot推送控制器-本地关井给平台";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ObjectMapper objectMapper = new ObjectMapper();
        JSONObject data;
        String create_time = "";

        create_time = simpleDateFormat.format(astLocalCloseWell.getCreate_time());
        data = objectMapper.convertValue(astLocalCloseWell, JSONObject.class);
        data.put("create_time", create_time);

        String requestBody = JSON.toJSONString(data);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON
            logger.info("信息:" + JSON.toJSONString(responseBody));

            String success = jsonObject.getString("success");
            String msg = jsonObject.getString("msg");

            if (StringUtils.equals("true", success)) {
                int i = ControllerLogUtils.setMsgLog("post", address,
                        controller_name, msg, requestBody, responseBody);
                astLocalCloseWell.setSuccess("true");
                int update = astLocalCloseWellMapper.update(astLocalCloseWell);
            } else {
                logger.info("错误信息:" + msg);
                int i = ControllerLogUtils.setErrorMsgLog("post", address,
                        controller_name, msg, requestBody, responseBody);
                astLocalCloseWell.setSuccess("flase");
                int update = astLocalCloseWellMapper.update(astLocalCloseWell);
            }

        } catch (Exception e) {
            logger.info("错误信息:" + e.getMessage());
            int i = ControllerLogUtils.setErrorMsgLog("post", address,
                    controller_name, e.getMessage(), requestBody, responseBody);
            astLocalCloseWell.setSuccess("flase");
            int update = astLocalCloseWellMapper.update(astLocalCloseWell);

            return null;
        }

        return null;
    }

    public String postIcCardStatus(String id, JSONObject preant) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/card/getCardStatus";

        preant.put("device_code", id);
        String requestBody = JSON.toJSONString(preant);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            Map<String, Object> map = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON

            String success = MapUtils.getString(map, "success", "");
            String msg = MapUtils.getString(map, "msg", "");
            if (StringUtils.equals("false", success)) {
                int i = ControllerLogUtils.setErrorMsgLog("post", address,
                        "获取设备端信息", msg, JSON.toJSONString(preant), JSON.toJSONString(responseBody));
                log.info("错误信息:" + msg);
                return null;
            }

            //发送数据
            boolean pub = mqttConnect.pub("/single/down/" + id, JSON.toJSONString(map));//发布主题

        } catch (Exception e) {
            int i = ControllerLogUtils.setErrorMsgLog("post", address,
                    "获取设备端信息", e.getMessage(), JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            log.info("错误信息:" + e.getMessage());
            return null;
        }

        int i = ControllerLogUtils.setMsgLog("post", address,
                "获取设备端信息", "成功", JSON.toJSONString(preant), JSON.toJSONString(responseBody));
        log.info("错误信息:" + JSON.toJSONString(responseBody));
        return null;
    }

    public String postGetValveControlResp(String id, JSONObject preant) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/updateContolValveStatus";

        String requestBody = JSON.toJSONString(preant);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            Map<String, Object> map = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON

            String success = MapUtils.getString(map, "success", "");
            String msg = MapUtils.getString(map, "msg", "");
            if (StringUtils.equals("false", success)) {
                int i = ControllerLogUtils.setErrorMsgLog("post", address,
                        "获取设备端信息", msg, JSON.toJSONString(preant), JSON.toJSONString(responseBody));
                log.info("错误信息:" + msg);
                return null;
            }

        } catch (Exception e) {
            int i = ControllerLogUtils.setErrorMsgLog("post", address,
                    "获取设备端信息", e.getMessage(), JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            log.info("错误信息:" + e.getMessage());
            return null;
        }

        int i = ControllerLogUtils.setMsgLog("post", address,
                "获取设备端信息", "成功", JSON.toJSONString(preant), JSON.toJSONString(responseBody));
        log.info("错误信息:" + JSON.toJSONString(responseBody));
        return null;
    }

    public String postUpdateContolValveStatus(String id, JSONObject preant) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/updateContolValveStatus";

        //处理数据 begin
        JSONObject data = preant.getJSONObject("data");
        int num = 1, son;
        String decoder, twoS;
        List<Map> list = new ArrayList();
        Map param;
        char[] twoC;

        for (int j = 1; j <= 8; j++) {
            decoder = MapUtils.getString(data, "decoder" + j, "0");
            //16进制转10进制
            son = Integer.parseInt(String.valueOf(decoder), 16);
            //10进制转2进制
            twoS = Integer.toBinaryString(son);
            //字符串倒序
            twoS = ControllerLogUtils.reverseString(twoS);
            //循环便利字符串
            for (int i = 0; i < 32; i++) {

                param = new LinkedHashMap();
                twoC=twoS.toCharArray();
                if (i + 1 <= twoC.length) {

                    param.put("decoder_adr", num);

                    if (StringUtils.equals("1", String.valueOf(twoC[i]))) {
                        param.put("state", "on");
                    } else {
                        param.put("state", "off");
                    }//if-else

                }else {
                    param.put("decoder_adr", num);
                    param.put("state", "off");
                }

                list.add(param);
                num++;

            }//for

        }//for
        preant.put("data",list);
        //处理数据 end

        String requestBody = JSON.toJSONString(preant);
        String responseBody = "";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            Map<String, Object> map = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON

            String success = MapUtils.getString(map, "success", "");
            String msg = MapUtils.getString(map, "msg", "");
            if (StringUtils.equals("false", success)) {
                //int i = ControllerLogUtils.setErrorMsgLog("post", address,"获取设备端信息", msg, JSON.toJSONString(preant), JSON.toJSONString(responseBody));
                log.info("错误信息:" + msg);
                return null;
            }

        } catch (Exception e) {
            //int i = ControllerLogUtils.setErrorMsgLog("post", address,"获取设备端信息", e.getMessage(), JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            log.info("错误信息:" + e.getMessage());
            return null;
        }

        //int i = ControllerLogUtils.setMsgLog("post", address,"获取设备端信息", "成功", JSON.toJSONString(preant), JSON.toJSONString(responseBody));
        log.info("错误信息:" + JSON.toJSONString(responseBody));
        return null;
    }

}
